//
//  HMVisitorLoginView.swift
//  SinaWeibo
//
//  Created by apple on 16/9/23.
//  Copyright © 2016年 itcast. All rights reserved.
//

import UIKit
//约定大于配置
//需要继承NSObjectProtocol  才可以使用weak关键字来修饰代理对象
@objc protocol HMVisitorLoginViewDelegate: NSObjectProtocol {
    //定义协议方法
    //登陆
    func userWillLogin()
    
    //注册 swift没有optional关键字
    @objc optional func userWillReigster()
}

class HMVisitorLoginView: UIView {
    
    //声明一个代理对象属性 需要使用weak修饰 防止循环引用
    weak var delegate: HMVisitorLoginViewDelegate?
    //对外提供外界设置信息的方法
    func updateInfo(tipText: String, imageName: String? = nil) {
        //给子控件更新相关的属性
        tipLabel.text = tipText
        //判断是否是首页
        if imageName == nil {
            //是首页 执行圈圈的动画效果
            startAnimation()
            
        } else {
            //不是首页
            circleView.image = UIImage(named: imageName!)
            //隐藏iconView
            iconView.isHidden = true
            bringSubview(toFront: circleView)
        }
        
    }
    
    
    //圈圈的动画效果
    private func startAnimation() {
        //使用基础动画
        let animation = CABasicAnimation(keyPath: "transform.rotation")
        //设置动画的相关参数
        //设置重复次数
        animation.repeatCount = MAXFLOAT
        //设置一个toValue 一圈
        animation.toValue = M_PI * 2
        //设置动画时间
        animation.duration = 15
        //isRemovedOnCompletion 当动画结束的时候或者当页面处于非活跃状态移除动画效果 默认值是true
        animation.isRemovedOnCompletion = false
        // 添加到圈圈的图层上
        circleView.layer.add(animation, forKey: nil)
    }
    
    //自定义方可视图 添加子视图
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    
    //解档默认实现报错
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    
    //设置UI界面
    private func setupUI() {
        addSubview(circleView)
        addSubview(backView)
        addSubview(iconView)
        addSubview(tipLabel)
        addSubview(loginBtn)
        addSubview(registerBtn)
        //circleView.translatesAutoresizingMaskIntoConstraints
        //make 实际上是取消了 translatesAutoresizingMaskIntoConstraints 属性的对象
        circleView.snp.makeConstraints { (make) in
            //make.centerX.equalTo(self.snp.centerX)
            make.centerX.equalTo(self)
            //make.centerY.equalTo(self.snp.centerY)
            make.centerY.equalTo(self).offset(-80)
            //make.center.equalTo(self)
        }
        iconView.snp.makeConstraints { (make) in
            make.center.equalTo(circleView)
        }
        
        backView.snp.makeConstraints { (make) in
            make.center.equalTo(circleView)
        }
        
        tipLabel.snp.makeConstraints { (make) in
            make.top.equalTo(circleView.snp.bottom).offset(16)
            make.centerX.equalTo(circleView)
            //如果需要自动换行的文字 不建议通过直接设置宽度的方式来约束 添加左右间距的约束
            //make.width.equalTo(224)
        }
        
        loginBtn.snp.makeConstraints { (make) in
            make.left.equalTo(tipLabel)
            make.top.equalTo(tipLabel.snp.bottom).offset(16)
            make.width.equalTo(100)
        }
        
        registerBtn.snp.makeConstraints { (make) in
            make.right.equalTo(tipLabel)
            make.top.equalTo(tipLabel.snp.bottom).offset(16)
            make.width.equalTo(100)
        }
        
        let value = CGFloat(237 / 255.0)
        backgroundColor = UIColor(red: value, green: value, blue: value, alpha: 1)
        
        loginBtn.addTarget(self, action: #selector(loginBtnDidClick), for: .touchUpInside)
        registerBtn.addTarget(self, action: #selector(registerBtnDidClick), for: .touchUpInside)
    }
    
    @objc private func loginBtnDidClick() {
        //使用代理对象调用 协议方法
        //判断代理对象是否为nil  是否响应该协议方法
        delegate?.userWillLogin()
    }
    
    @objc private func registerBtnDidClick() {
        //可选的协议方法 可以实现 也可以不实现
        //必选的协议方法 必须要实现
        delegate?.userWillReigster?()
    }
    
    //懒加载子视图
    //转动的圈圈
    private lazy var circleView: UIImageView = UIImageView(image: #imageLiteral(resourceName: "visitordiscover_feed_image_smallicon"))
    
    //大房子
    private lazy var iconView: UIImageView = UIImageView(image: #imageLiteral(resourceName: "visitordiscover_feed_image_house"))
    
    //遮罩
    private lazy var backView: UIImageView = UIImageView(image: #imageLiteral(resourceName: "visitordiscover_feed_mask_smallicon"))
    
    //提示文字
    private lazy var tipLabel: UILabel = {
        let l = UILabel(title: "哈哈哈哈哈哈哈啊哈哈哈哈啊哈哈哈啊哈哈哈哈哈", textColor: UIColor.darkGray, fontSize: 14)
        //换行 1. 限制宽度  2.设置自动换行
        l.numberOfLines = 0
        //设置最大的布局宽度
        l.preferredMaxLayoutWidth = 224
        //自适应大小
        //设置居中对齐
        l.textAlignment = .center
        return l
    }()
    
    //注册按钮
    private lazy var registerBtn : UIButton = {
        let btn = UIButton(title: "注册", textColor: UIColor.orange, fontSize: 14)
        //设置背景图片
        btn.setBackgroundImage(#imageLiteral(resourceName: "common_button_white"), for: .normal)
        return btn        
    }()
    //登陆按钮
    
    private lazy var loginBtn : UIButton = {
        let btn = UIButton(title: "登陆", textColor: UIColor.darkGray, fontSize: 14)
        //设置背景图片
        btn.setBackgroundImage(#imageLiteral(resourceName: "common_button_white"), for: .normal)
        return btn
    }()
}
